<?php $gridId=rand(0,10000); ?>
<link rel="stylesheet" type="text/css" href='<?=$this->baseUrl?>/public/css/lightbox.css' />
<script type="text/javascript">
Ext.onReady(function(){
var website="<?=$this->website?>";
Ext.BLANK_IMAGE_URL = '<?=$this->baseUrl?>/public/images/default/s.gif';
Ext.ux.Lightbox.register('a[rel^=lightbox]');
Ext.ux.Lightbox.register('a.lb-flower', true); // true to show them as a set

//定义详细信息插件
var expander = new Ext.grid.RowExpander({
        tpl : new Ext.Template(
            '<p><b>重量:</b> {weight}</p><br>',
            '<p><b>URL key:</b>{url_key}</p>',
			'<p><b>meta title:</b>{meta_title}</p>'
        )
    });
//定义数据源
var ds=new Ext.data.Store({
							  proxy:new Ext.data.HttpProxy({
									url:'<?=$this->baseUrl?>/site/productdata/id/<?=$this->id;?>'
									}),
							  reader:new Ext.data.JsonReader({
									root:'results',
									totalProperty:'total'
									},[
									   {name:'product_id',mapping:'entity_id'},
									   {name:'name',mapping:'name'},
									   {name:'price',mapping:'price'},
									   {name:'url_path',mapping:'url_path'},
									   {name:'images',mapping:'image'},
									   {name:'weight',mapping:'weight'},
									   {name:'url_key',mapping:'url_key'},
									   {name:'meta_title',mapping:'meta_title'}
									   ])
	});		
	
//定义列模型
var cm=new Ext.grid.ColumnModel([
									 //new Ext.grid.CheckboxSelectionModel({singleSelect:false}),
									  expander,
									 {id:'product_id',header:'编号',dataIndex:'product_id',width:60},
									 {header:'产品名',dataIndex:'name',width:100},
									 {header:'价格',dataIndex:'price',width:100,renderer:show_money},
									 {header:'链接',dataIndex:'url_path',width:150,renderer:show_link},
									 {header:'图片',dataIndex:'images',width:100,renderer:show_img}
	]);
cm.defaultSortable = true; 

 



//每页显示记录数
	var mypagedata=[
				 ['10','10'],
				 ['15','15'],
				 ['20','20'],
				 ['40','40'],
				 ['200','200']
				 ];	
var page_size_store=new Ext.data.SimpleStore({
									   fields:[
											   {name:'id'},
											   {name:'name'},
									   		  ]
									   });
page_size_store.loadData(mypagedata);
	var pagesize_combo=new Ext.form.ComboBox({
										 store:page_size_store,
										 width:50,										
										 emptyText:'10',
										 mode:'local',
										 triggerAction:'all',
										 valueField:'name',
										 displayField:'id'
										 });
	pagesize_combo.on('select',function(e){										
										var pagesize=parseInt(e.getValue());
										pagBar.pageSize=pagesize;
										ds.load({params:{start:0,limit:pagesize}});
										//grid.render();
										});									   
//定义分页条栏				 
var pagBar=new Ext.PagingToolbar({
										pageSize:10,
										store:ds,
										displayInfo:true,
										displayMsg:'第{0}到{1}条产品 共{2}条产品',
										emptyMsg:'no content',
										items:['-','每页显示',pagesize_combo,'条记录','-']
									 });


var menu_explode=new Ext.Button({
								text:'导出至',
								menu:[
								{text:'shopzilla',
								menu:{
									items:[
											{text:'所有产品',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/shopzilla';}},
											{text:"配件",handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/shopzilla/category/accessory';}}
										   ]									
									}
								},
								{text:'shopping',
								menu:{
									items:[
											{text:'所有产品',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/shopping';}},
											{text:"配件",handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/shopping/category/accessory';}}
										   ]									
									}
								},
								{text:'thefind',
								menu:{
									items:[
											{text:'所有产品',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/thefind';}},
											{text:"配件",handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/thefind/category/accessory';}}
										   ]									
									}
								},
								{text:'Google',
								menu:{
									items:[
											{text:'所有产品',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/google';}},
											{text:"配件",handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/google/category/accessory';}}
										   ]									
									}
								}]
								});
//定义grid菜单栏
//var menu_explode=new Ext.Button({
//	text:'导出至',
//	menu:[
//		{text:'shopzilla',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/shopzilla';}},
//		{text:'shopping',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/shopping';}},
//		{text:'thefind',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/thefind';}},
//		{text:'Google',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/google';}},
//		"-",
//		{text:'配件导出',
//		menu:{
//			items:[
//			  	{text:'shopzilla',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/shopzilla/category/accessory';}},
//				{text:'shopping',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/shopping/category/accessory';}},
//				{text:'thefind',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/thefind/category/accessory';}},
//				{text:'Google',handler:function(){location.href='<?=$this->baseUrl?>/data/explode/id/<?=$this->id?>/type/google/category/accessory';}}
//			  ]}
//		}
//	]
//});	


//定义容器
var grid=new Ext.grid.GridPanel({
									renderTo:'grid_<?=$gridId?>',
									store:ds,
									cm:cm,	
									minHeight:300,									
									height:500,
									autoHeight:true,
									autoWidth:true,									
									loadMask:{msg:'加载服务端数据中',store:ds},									
									autoScroll:true,
									stripeRows:true,									
        							animCollapse: false,
									//sm:new Ext.grid.CheckboxSelectionModel({singleSelect:false}),
									monitorWindowResize: true,								
									title:'产品列表',
									plugins: expander,
									tbar:[
										 /*{
										  id:'add',
										  text:'新增',
										  iconCls:'icn-add',
										  handler:order_add
										  },
										  {
											text:'修改',
											iconCls:'icn-edit',
											handler:function(){
												var count=grid.getStore().getCount();
												var selectRows=grid.getSelectionModel().getSelections();												
												if(selectRows.length>1 || selectRows.length===0){
													Ext.MessageBox.alert('提示','请选择一条订单');
													}
												else{
													Ext.MessageBox.alert('执行','你选择的是第'+selectRows[0].get('product_id')+'条数据');
													}
											}
										  },
										  {
											text:'删除',
											iconCls:'icn-delete',
											handler:function(){												
													var selectRows=grid.getSelectionModel().getSelections();												
												if(selectRows.length==0){
													Ext.MessageBox.alert('提示','请选择一条订单');
													}
												else{
													var message="你选择的是第";
													for(var i=0;i<selectRows.length;i++){
														message+=selectRows[i].get('entity_id')+",";
														}
													Ext.MessageBox.alert('执行','你选择的是第'+message+'条数据');
													}
												}
											  },{xtype:'tbfill'},*/menu_explode],
									bbar:pagBar
									});
	
//显示数据
ds.load({params:{start:0,limit:15}});
grid.render();
grid.addListener('rowcontextmenu', rightClickFn);
grid.addListener('rowdblclick', gridRowDBLClick);

//定义grid右键菜单
var menu_right=new Ext.menu.Menu({
	id:'right_click',
	items:[
			{id:'rMenu1',text:'展开/伸缩',iconCls:'icn-folder-new',handler:gridRowExpand},
			{id:'rMenu2',text:'全部展开',iconCls:'icn-refresh',handler:gridAllRowExpand}
			]
	});
function rightClickFn(client,rowIndex,e){
	e.preventDefault();
	if(rowIndex<0){return;} 	
    menu_right.showAt(e.getXY());
}

//行展开伸缩,点击
function gridRowDBLClick(grid,rowIndex,e){
	expander.toggleRow(rowIndex);
	}

//行展开伸缩，右键菜单
function gridRowExpand(){
	//var selectRows=grid.getSelectionModel().getSelections();
//	alert(selectRows.length);
//	if(selectRows.length==1){
//			Ext.MessageBox.alert('提示','请选择一条订单');
//		}
	
}
	
function gridAllRowExpand(){
	//expander.toggleRow(rowIndex);
	}

});

function order_add(){
	            win = new Ext.Window({
                applyTo     : 'order_add',
                layout      : 'fit',
                width       : 500,
                height      : 300,
                closeAction :'hide',
                plain       : true,
                items       : new Ext.TabPanel({
                    applyTo        : 'hello-tabs',
                    autoTabs       : true,
                    activeTab      : 0,
                    deferredRender : false
                }),

                buttons: [{
                    text     : '提交'
                },{
                    text     : 'Close',
                    handler  : function(){
                        win.hide();
                    }
                }]
            });
				win.show();	
        }
        
function show_link(val){
	return "<a href='<?=$this->website?>/"+val+"' target='_blank'>"+val+"</a>";
}
function show_money(val){
	if(parseInt(val)===0 || val===null){
		return "<span style='color:red'>"+val+"</span>";
	}
	return "$"+val+" USD";
}
function show_img(val){
	//return "<span style='display:table' qtip='<img src=\'<?=$this->website?>/media/catalog/product/"+val+"\''>"+val+"</span>";	
	var link='<?=$this->website?>/media/catalog/product/'+val;
	return "<a href='"+link+"' class='lb-flower'><img src='"+link+"' width='50' height='30' /></a>";
}
</script>
<body style="width:98%;height:94%">
	<div id='grid_<?=$gridId?>'></div>
</body>
</html>